<?php
/**
 * 增强版网站地图生成器
 * 包含首页、分类页面和所有导航项目
 */

// 设置正确的路径和包含
define('IN_SITEMAP', true);

// 获取当前工作目录并设置正确的路���
$currentDir = dirname(__DIR__);
require_once $currentDir . '/config/config.php';
require_once $currentDir . '/includes/database.php';

// 设置响应头 - 使用SEO配置常量
header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: public, max-age=' . SEO_CACHE_MAXAGE_SITEMAP);

// 初始化数据库
$db = new Database();
$categories = $db->getCategories();
$allNavigations = [];

// 获取所有导航数据
foreach ($categories as $category) {
    $navs = $db->getNavigations($category['id']);
    $allNavigations[$category['id']] = $navs;
}

// 获取网站设置
$settings = $db->getSettings();
$siteTitle = $settings['site_title'] ?? SITE_TITLE;
$siteUrl = SITE_URL;

// 开始XML输出
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

    <!-- 首页 -->
    <url>
        <loc><?php echo htmlspecialchars($siteUrl); ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq><?php echo SEO_SITEMAP_CHANGE_FREQ_HOMEPAGE; ?></changefreq>
        <priority><?php echo SEO_SITEMAP_PRIORITY_HOMEPAGE; ?></priority>
        <xhtml:link rel="alternate" hreflang="zh-CN" href="<?php echo htmlspecialchars($siteUrl); ?>"/>

        <!-- 首页图标 -->
        <?php if (!empty($settings['favicon_path'])): ?>
        <image:image>
            <image:loc><?php echo htmlspecialchars($siteUrl . ltrim($settings['favicon_path'], './')); ?></image:loc>
            <image:title><?php echo htmlspecialchars($siteTitle); ?> 网站图标</image:title>
            <image:caption><?php echo htmlspecialchars($siteTitle); ?>的官方网站图标</image:caption>
        </image:image>
        <?php endif; ?>
    </url>

    <!-- 分类页面 -->
    <?php foreach ($categories as $category): ?>
    <url>
        <loc><?php echo htmlspecialchars($siteUrl . '#' . generateSEOFriendlyUrl($category['name'])); ?></loc>
        <lastmod><?php echo htmlspecialchars($category['updated_at'] ?? date('Y-m-d')); ?></lastmod>
        <changefreq><?php echo SEO_SITEMAP_CHANGE_FREQ_CATEGORY; ?></changefreq>
        <priority><?php echo SEO_SITEMAP_PRIORITY_CATEGORY; ?></priority>
        <xhtml:link rel="alternate" hreflang="zh-CN" href="<?php echo htmlspecialchars($siteUrl . '#' . generateSEOFriendlyUrl($category['name'])); ?>"/>

        <!-- 分类图标 -->
        <?php if (!empty($category['icon']) && isset($category['icon_type']) && $category['icon_type'] === 'url'): ?>
        <image:image>
            <image:loc><?php echo htmlspecialchars($siteUrl . ltrim($category['icon'], './')); ?></image:loc>
            <image:title><?php echo htmlspecialchars($category['name']); ?> 分类图标</image:title>
            <image:caption><?php echo htmlspecialchars($category['name'] . '分类的图标'); ?></image:caption>
        </image:image>
        <?php endif; ?>
    </url>
    <?php endforeach; ?>

    <!-- 导航网站 -->
    <?php foreach ($allNavigations as $categoryId => $navigations): ?>
        <?php
        // 获取对应分类信息
        $currentCategory = null;
        foreach ($categories as $cat) {
            if ($cat['id'] == $categoryId) {
                $currentCategory = $cat;
                break;
            }
        }

        foreach ($navigations as $nav): ?>
        <url>
            <loc><?php echo htmlspecialchars($nav['url']); ?></loc>
            <lastmod><?php echo htmlspecialchars($nav['updated_at'] ?? date('Y-m-d')); ?></lastmod>
            <changefreq><?php echo SEO_SITEMAP_CHANGE_FREQ_NAVIGATION; ?></changefreq>
            <priority><?php echo SEO_SITEMAP_PRIORITY_NAVIGATION; ?></priority>

            <!-- 如果有图片，添加图片信息 -->
            <?php if (!empty($nav['icon']) && $nav['icon_type'] === 'url'): ?>
            <image:image>
                <image:loc><?php echo htmlspecialchars($siteUrl . ltrim($nav['icon'], './')); ?></image:loc>
                <image:title><?php echo htmlspecialchars($nav['name']); ?> Logo</image:title>
                <image:caption><?php echo htmlspecialchars($nav['description'] ?? $nav['name'] . '网站logo'); ?></image:caption>
            </image:image>
            <?php endif; ?>

            <!-- 多语言链接 -->
            <xhtml:link rel="alternate" hreflang="zh-CN" href="<?php echo htmlspecialchars($nav['url']); ?>"/>
            <xhtml:link rel="alternate" hreflang="en" href="<?php echo htmlspecialchars($nav['url']); ?>"/>

            <!-- 分类关联信息 -->
            <?php if ($currentCategory): ?>
            <xhtml:link rel="related" href="<?php echo htmlspecialchars($siteUrl . '#' . generateSEOFriendlyUrl($currentCategory['name'])); ?>"/>
            <?php endif; ?>
        </url>
        <?php endforeach; ?>
    <?php endforeach; ?>

</urlset>
<?php exit; ?>